package com.upb.webserver.contract;

import com.upb.webserver.account.AccountService;
import com.upb.webserver.common.pojo.account.AccountCommon;
import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.pojo.contract.ReqSendTransVO;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * controller of contract.
 */
@Slf4j
@RestController
@RequestMapping(value = "contract")
public class ContractController {
    @Autowired
    private ContractManager contractManager;
    @Autowired
    private ContractService contractService;
    @Autowired
    private AccountService accountService;


    /**
     * @param pageNumber
     * @param pageSize
     * @param chainId
     * @param contractStatus
     * @param chainType
     * @param applicationId
     * @param request
     * @return
     */
    @GetMapping(value = "/list")
    public BasePageResponse queryChainCodeList(@RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                               @RequestParam(value = "pageSize", required = false, defaultValue = "50") Integer pageSize,
                                               @RequestParam(value = "chainId", required = false) Integer chainId,
                                               @RequestParam(value = "contractStatus", required = false) Byte contractStatus,
                                               @RequestParam(value = "chainType", required = false, defaultValue = "-1") Byte chainType,
                                               @RequestParam(value = "applicationId", required = false) Integer applicationId,
                                               @RequestParam(value = "containDetailFields", required = false, defaultValue = "false") Boolean containDetailFields,
                                               HttpServletRequest request) {
        log.info("start exec method [queryContractList]. pageNumber:{} pageSize:{} chainId:{} contractStatus:{} chainType:{} applicationId:{} containDetailFields:{}", pageNumber, pageSize, chainId, contractStatus, chainType, applicationId, containDetailFields);
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        BasePageResponse basePageResponse = contractService.queryByPage(accountCommon.getPlatform(), accountCommon.getId(), pageNumber, pageSize, chainId, contractStatus, chainType, applicationId, containDetailFields);
        log.info("success exec method [queryContractList].");
        log.debug("success exec method [queryContractList]. result:{}", JsonUtils.objToString(basePageResponse));
        return basePageResponse;
    }

    /**
     * @param contractId
     * @return
     */
    @DeleteMapping(value = "/{applicationId}/{contractId}")
    public BaseResponse deleteContract(@PathVariable("applicationId") Integer applicationId, @PathVariable("contractId") Integer contractId) {
        log.info("start exec method [deleteContract]. applicationId:{} contractId:{}", applicationId, contractId);
        contractService.deleteContract(applicationId, contractId);
        log.info("success exec method [deleteContract].");
        return new BaseResponse(ConstantCode.SUCCESS);
    }


    /**
     * send transasaction.
     *
     * @param param
     * @param result
     * @return
     */
    @PostMapping(value = "/transaction")
    public BaseResponse sendTransaction(@RequestBody @Valid ReqSendTransVO param, BindingResult result) {
        log.info("start exec method [sendTransaction]. param:{}", JsonUtils.objToString(param));
        BaseResponse baseResponse = contractService.sendTransaction(param);
        log.info("success exec method [sendTransaction]. result:{}", JsonUtils.objToString(baseResponse));
        return baseResponse;
    }
}
